<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once 'page.php';
require_once 'login.php';
require_once 'Gallery.php';
require_once 'ImagesAdmin.php';
/**
 * @author Garry
 *
 *
 */
class admin extends page  {
	/**
	 * @author Garry Willits
	 * @copyright 
	 * @date 30th April 2011
	 */
	//TODO - Insert your code here
	public function index(){
		$this->verify();
		$display = new AdminDisplay();
		$display->selected = "";
		$display->finalize();
		parent::addChild($display,'main'); 
		parent::output(  ); 
		
	}
	public function site( ){
		$this->verify();
		$display = new AdminDisplay();
		$display->selected = "site";
		$siteadmin = new SiteAdmin();
		$siteadmin->display->finalize();
		$display->admincontent = $siteadmin->display->content;
		$display->finalize();
		parent::addChild($display,'main'); 
		parent::output(  );  
	}
	
	public function images( ){
		$this->verify();
		$display = new AdminDisplay();
		$display->selected = "images";
		$imagesadmin = new ImagesAdmin();
		$buffer = "";
		if(!$imagesadmin->working ){
			$gallery = new Gallery(Gallery::EDIT,Gallery::MULTI);
			$buffer .= $gallery->display->content;
		}
		$buffer .= $imagesadmin->display->content;
		$display->admincontent = $buffer;
		$display->finalize();
		parent::addChild($display,'main'); 
		parent::output(  );  
	}
	
	public function editImage( $id,$catid,$pageNum ){
		$this->verify();
		$display = new AdminDisplay();
		$display->selected = "images";
		$editimage = new EditImage($id,$catid,$pageNum);
		$buffer = "";
		$buffer .= $editimage->display->content;
		$display->admincontent = $buffer;
		$display->finalize();
		parent::addChild($display,'main'); 
		parent::output(  );  
	}

	private function verify(){
		session_start();
		if( empty($_SESSION['logged_in']) && $_SESSION['logged_in']!= TRUE ){
			$this->logout();
		}
	}
	/**
	 * 
	 * Effectively the home page
	 */
	public function login()
	{
		$login = new Login('admin/site');
		parent::addChild($login->display,'main'); 
		parent::output(  ); 
	}
	
	/**
	 * 
	 * Logs out the user and destroys the session
	 */
	public function logout()
	{
		session_start();
		session_unset();
		session_destroy();
		$_SESSION = array();
		$_SESSION['logged_in']= FALSE;
		redirect('');
	}
}


?>